/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.uct.service.plugins  
 */
package com.apache.uct.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.tools.StrUtil;
import com.apache.uct.common.entity.Org;
import com.apache.uct.common.entity.User;
import com.apache.uct.util.UctStringUtil;

public class OrgListPlugin implements PluginConnector {

	private Logger log = Logger.getLogger(UserListPlugin.class);
	private IDao orgDao;
	private IDao userDao;

	/**
	 * TODO 获取用户列表插件(参数中需要有当前用户的ID). <br>
	 * 参数说明:<br>
	 *  orgIds:ID集合;按ID集合查询机构列表;<br>
	 *  org属性:按机构属性查询机构列表;<br>
	 *  分页设置:<br>
	 *  pageSize:每页记录数;<br>
	 *  pageIndex:当前页码;
	 */
	public Object execute(ParamsVo vo) throws Exception {
		String orderBy = "create_time";
		String orderByParam = String.valueOf(vo.getParams("orderBy"));
		if (StrUtil.isNotNull(orderByParam)) {
			orderBy = orderByParam;
		}
		ResultEntity result = new ResultEntity();
		try {
			result.setResult("true");
			String orgIds = String.valueOf(vo.getParams("orgIds"));
			if (Validator.isNull(orgIds)) {
				MethodParam param = setMethodParams(vo, 2, orderBy);
				if (Validator.isEmpty(param)) {
					result.setResult("false");
					result.setMessage("查询条件为空或不存在相关信息");
					return result;
				}
				int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
				int pageIndex = Integer
						.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
				if (pageSize == 0 && pageIndex == 0) {
					param.setOrderby(orderBy + " desc");
					param.setParams("orderBy", param.getOrderby());
					result.setEntity(orgDao.select(param));
				} else {
					param.setPageIndex(pageIndex);
					param.setPageSize(pageSize);
					param.setOrderby(orderBy + " desc");
					param.setParams("orderBy", param.getOrderby());
					Page page = orgDao.pageSelect(param);
					result.setEntity(page);
				}
			} else {
				StringBuffer sb = new StringBuffer(orgDao.getSql(2));
				MethodParam param = new MethodParam("ByProperty", "", "", Org.class.getName());
				String[] orgArry = orgIds.split(",");
				StringBuffer returnOrgIds = new StringBuffer();
				int count = orgArry.length;
				for (int i = 0; i < count; i++) {
					if (i == count - 1) {
						returnOrgIds.append("'").append(orgArry[i]).append("'");
					} else {
						returnOrgIds.append("'").append(orgArry[i]).append("',");
					}
				}
				String hql = returnOrgIds.toString();
				param.setParams("orgIds", hql);
				//				param.setParams("orgStatus", "1");
				result.setEntity(orgDao.select(param));
			}
			result.setMessage("查询成功");
		} catch (Exception e) {
			result.setResult("false");
			log.error("执行机构查询失败 ", e);
			result.setMessage("机构查询失败！");
		}
		return result;
	}

	private MethodParam setMethodParams(ParamsVo vo, int type, String orderBy) {
		StringBuffer sb = new StringBuffer(orgDao.getSql(type));
		MethodParam param = new MethodParam("ByProperty", "", "", Org.class.getName());
		String orgEname = String.valueOf(vo.getParams("orgEname"));
		String orgCname = String.valueOf(vo.getParams("orgCname"));
		String fatherId = String.valueOf(vo.getParams("fatherId"));
		if (Validator.isNotNull(orgEname)) {
			param.setParams("orgEname", orgEname);
		}
		if (Validator.isNotNull(orgCname)) {
			param.setParams("orgCname", orgCname);
		}
		//		if (Validator.isNotNull(fatherId)) {
		//			param.setParams("fatherId", fatherId);
		//		}

		String all = String.valueOf(vo.getParams("orgAll"));//获取所有机构列表
		if (Validator.isNull(all)) {
			String str = buildParams(param, vo);
			if (Validator.isNull(str)) {
				return null;
			}
			sb.append(str);
		}
		param.setParams("orgStatus", "1");
		param.setSqlStr(sb.toString());
		return param;
	}

	private String buildParams(MethodParam param, ParamsVo vo) {
		StringBuffer sb = new StringBuffer();
		if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
			sb.append(" and sysEname = :sysEname");
			param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgDomain")))) {
			sb.append(" and orgDomain = :orgDomain");
			param.setParams("orgDomain", String.valueOf(vo.getParams("orgDomain")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgCname")))) {
			sb.append(" and orgCname like '%" + String.valueOf(vo.getParams("orgCname")) + "%'");
			param.setParams("orgCname", "'%" + String.valueOf(vo.getParams("orgCname")) + "%'");
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgEname")))) {
			sb.append(" and orgEname = :orgEname");
			param.setParams("orgEname", String.valueOf(vo.getParams("orgEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fullEname")))) {
			sb.append(" and fullEname = :fullEname");
			param.setParams("fullEname", String.valueOf(vo.getParams("fullEname")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fullCname")))) {
			sb.append(" and fullCname = :fullCname");
			param.setParams("fullCname", String.valueOf(vo.getParams("fullCname")));
		}

		if (Validator.isNotNull(String.valueOf(vo.getParams("orgCode")))) {
			sb.append(" and orgCode = :orgCode");
			param.setParams("orgCode", String.valueOf(vo.getParams("orgCode")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("boss")))) {
			sb.append(" and boss = :boss");
			param.setParams("boss", String.valueOf(vo.getParams("boss")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgType")))) {
			sb.append(" and orgType = :orgType");
			param.setParams("orgType", String.valueOf(vo.getParams("orgType")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgRegMoney")))) {
			sb.append(" and orgRegMoney = :orgRegMoney");
			param.setParams("orgRegMoney", String.valueOf(vo.getParams("orgRegMoney")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgScope")))) {
			sb.append(" and orgScope = :orgScope");
			param.setParams("orgScope", String.valueOf(vo.getParams("orgScope")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("homepage")))) {
			sb.append(" and homepage = :homepage");
			param.setParams("homepage", String.valueOf(vo.getParams("homepage")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("linkMan")))) {
			sb.append(" and linkMan = :linkMan");
			param.setParams("linkMan", String.valueOf(vo.getParams("linkMan")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("phone")))) {
			sb.append(" and phone = :phone");
			param.setParams("phone", String.valueOf(vo.getParams("phone")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("fax")))) {
			sb.append(" and fax = :fax");
			param.setParams("fax", String.valueOf(vo.getParams("fax")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("mobile")))) {
			sb.append(" and mobile = :mobile");
			param.setParams("mobile", String.valueOf(vo.getParams("mobile")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("email")))) {
			sb.append(" and email = :email");
			param.setParams("email", String.valueOf(vo.getParams("email")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("province")))) {
			sb.append(" and province = :province");
			param.setParams("province", String.valueOf(vo.getParams("province")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("city")))) {
			sb.append(" and city = :city");
			param.setParams("city", String.valueOf(vo.getParams("city")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("address")))) {
			sb.append(" and address = :address");
			param.setParams("address", String.valueOf(vo.getParams("address")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("postCode")))) {
			sb.append(" and postCode = :postCode");
			param.setParams("postCode", String.valueOf(vo.getParams("postCode")));
		}
		if (Validator.isNotNull(String.valueOf(vo.getParams("orgRemark")))) {
			sb.append(" and orgRemark = :orgRemark");
			param.setParams("orgRemark", String.valueOf(vo.getParams("orgRemark")));
		}

		String userId = String.valueOf(vo.getParams("userId"));
		if (Validator.isNotNull(userId)) {
			String str = buildParams(param, vo, userId);
			sb.append(str);
		} else {
			String fatherId = String.valueOf(vo.getParams("fatherId"));
			if (Validator.isNotNull(fatherId)) {
				sb.append(" and fatherId = :fatherId");
				param.setParams("fatherId", fatherId);
			}
		}
		return sb.toString();
	}

	//开启授权机制
	private String buildParams(MethodParam param, ParamsVo vo, String userId) {
		StringBuffer sb = new StringBuffer("");
		String fatherId = String.valueOf(vo.getParams("fatherId"));
		if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {//是否开启机构授权
			MethodParam params = new MethodParam("ById", "", "", User.class.getName());
			params.setInfoId(userId);
			User user = (User) userDao.selectById(params);
			String sysFlag = UctStringUtil.getSysFlag(user.getSysEname(), user.getDelStatus());
			String isSysUser = Validator.getDefaultStr(sysFlag, "0");
			if (Validator.isNotNull(isSysUser) && !"1".equals(isSysUser)) {//非系统用户
				String userOrgId = user.getOrgId();
				if ("1".equals(SystemTools.getInstance().getValue("manager_suborg"))) {//可以管理子机构
					if (Validator.isNotNull(fatherId) && "0".equals(fatherId)) {
						sb.append(" and orgId = :orgId");
						param.setParams("orgId", userOrgId);
					} else if (Validator.isNotNull(fatherId)) {
						sb.append(" and fatherId = :fatherId");
						param.setParams("fatherId", fatherId);
					}
				} else {
					sb.append(" and orgId = :orgId");
					param.setParams("orgId", userOrgId);
				}
			} else {//系统用户操作
				if (Validator.isNotNull(fatherId)) {
					sb.append(" and fatherId = :fatherId");
					param.setParams("fatherId", fatherId);
				}
			}
		} else {
			if (Validator.isNotNull(fatherId)) {
				sb.append(" and fatherId = :fatherId");
				param.setParams("fatherId", fatherId);
			}
		}
		return sb.toString();
	}

	public void setOrgDao(IDao orgDao) {
		this.orgDao = orgDao;
	}

	public void setUserDao(IDao userDao) {
		this.userDao = userDao;
	}

}
